package org.opens.javaskill.thread.create;

import lombok.Data;

import java.util.concurrent.*;

public class UseCallAble {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<String> submit = executorService.submit(new TaskWithResult(23));
        // 检测线程任务是否完成
        System.out.println(submit.isDone());
        // 该方法会如果线程任务没有执行完就调用, 就会陷入阻塞
        System.out.println(submit.get());
        System.out.println(submit.isDone());
        executorService.shutdown();
    }

}

@Data
class TaskWithResult implements Callable<String> {

    private int id;

    public TaskWithResult(int id) {
        this.id = id;
    }

    @Override
    public String call() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "s" + id;
    }
}
